package com.suixun.document.impl;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

import com.authority.dao.AuthUserDataDao;
import com.suixun.document.XmlDocument;
import com.suixun.entity.RegionInfo;
import com.suixun.util.StringUtil;
/**
 * 
 * <p>Title: RegionParser</p>
 * <p> 功能描述:查询省市区 </p> 
 * @author	陈显强
 * @date	2017年4月14日上午11:33:54
 * @version 1.0
 */
@Component
public class RegionParser implements XmlDocument<RegionInfo,List<RegionInfo>> {
	@Resource
	private AuthUserDataDao authUserDataDao;
	
	@Override
	@SuppressWarnings("rawtypes")
	public List<RegionInfo> parserXml(String fileName,RegionInfo regionInfo) throws DocumentException{
		String filePath = this.getClass().getClassLoader().getResource(fileName).getPath();
		File inputXml = new File(filePath);
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputXml);
        Element regions = document.getRootElement();
        List<RegionInfo> regionList = new LinkedList<>();
        RegionInfo region = null;
        if (regionInfo != null) {
        	if (regionInfo.getUserid() != null) {
        		//权限限制
        		return getRegionsByUserId(regionInfo,regionList);
        	}
        	 for (Iterator i = regions.elementIterator(); i.hasNext();) {
        		 Element province = (Element) i.next();
        		 if (regionInfo.getType() == 1) {
        			 region = new RegionInfo();
        			 region.setProvince(province.attributeValue("name"));
        			 regionList.add(region);
        			 continue;
        		 }
        		 if (regionInfo.getType() == 2) {
        			 if (province.attributeValue("name").equals(regionInfo.getProvince())) {
        				 for (Iterator j = province.elementIterator(); j.hasNext();) {
        					 region = new RegionInfo();
                             Element city = (Element) j.next();
                             region.setCity(city.attributeValue("name"));
                             regionList.add(region);
                         }
        				 break;
        			 }
        		 }
        		 if (regionInfo.getType() == 3) {
        			 for (Iterator j = province.elementIterator(); j.hasNext();) {
        				 Element city = (Element) j.next();
        				 if (city.attributeValue("name").equals(regionInfo.getCity())) {
        					 for (Iterator k = city.elementIterator(); k.hasNext();) {
        						 region = new RegionInfo();
        						 Element dis = (Element) k.next();
    							 region.setTown(dis.attributeValue("name"));
    							 regionList.add(region);
        					 }
        					 break;
        				 }
        			 }
        		 }
             }
        }
		return regionList;
	}

	private List<RegionInfo> getRegionsByUserId(RegionInfo regionInfo,List<RegionInfo> regionList) {
		Map<String,Object> map = new HashMap<>();
		map.put("userId", regionInfo.getUserid());
		if (StringUtil.isNotEmpty(regionInfo.getProvince())) {
			map.put("province", regionInfo.getProvince());
		}
		if ("3".equals(String.valueOf(regionInfo.getType()))) {
			if (StringUtil.isNotEmpty(regionInfo.getCity())) {
				map.put("city", regionInfo.getCity());
			}
		}
		List<RegionInfo> userRegionList = authUserDataDao.getRegionByUserId(map);
		if (userRegionList.size() == 0) {
			return regionList;
		}
		for (RegionInfo region : userRegionList) {
			regionList.add(region);
		}
		return regionList;
	}
}
